package com.buba.service.impl;

import com.buba.mapper.DepartmentMapper;
import entity.staff.Department;
import com.buba.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-03-14
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    /* *
     * @Author: beisheng
     * @Descriprion: 树形查询所有部门
     * @Date: 2023/3/16 14:29
     * @Return: common.R
     * @Param: []
     */

    @Override
    public List<Department> selAllDepartTree() {
        List<Department> departmentList = departmentMapper.selectList(null);
        List<Department> collect = departmentList.stream().filter(i ->
                Integer.valueOf(i.getRankId())  == 0
        ).map(menu -> {
            menu.setChildren(getChidrens(menu,departmentList));
            return menu;
        }).collect(Collectors.toList());
        return collect;
    }

    public List<Department> getChidrens(Department root,List<Department> all){
        List<Department> children  = all.stream().filter(department -> {
            return Integer.valueOf(department.getRankId()).equals(root.getOzId());
        }).map(department -> {
            department.setChildren(getChidrens(department,all));
            return department;
        }).collect(Collectors.toList());
        return children;
    }
}
